import {Request} from "../../utils/request";
import host from "../../utils/host";
import func from "../../utils/functions";
const Func = new func();
const HostUrl = host.service;
const Requ = new Request();
var app = getApp();
var abc
var num = 0
Page({
  data: {
    winHeight: 2500,
    currentTab: 0,
  },
  onLoad: function () {
    clearInterval(this.data.myintervalid);
    this.setData({
      navH: app.globalData.navHeight,
    })

  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
    var number = app.globalData.version
    var that = this;
    this.GetUserOrderList(that.data.currentTab);
    return;
    new Request('get_user_orderlist.cls.php').request(
      () => {
        return {
          data: that.data.currentTab,
          num: 0,
          number: number,
          openid: app.globalData.openid,
        }
      },
      (res) => {
		  console.log(res)
        if(res.data['order'] == undefined){
          Requ.showToast('暂无订单');
          this.setData({
            data: res.data,
          });
          return;
        }
        var order = res.data.order
        var orderH = order.length * 360
        that.setData({
          data: res.data,
          order: res.data.order,
          winHeight: orderH
        });
      });
  },
  //获取用户分类订单列表数据
  GetUserOrderList: function(current){
    new Request('get_user_orderlist.cls.php').request(
      () => {
        return {
          data: 0,
          num: num,
          number: app.globalData.version,
          openid: app.globalData.openid,
        }
      },
      (res) => {
        this.setData({
          data: res.data,
        })
        wx.hideLoading();
        if(res.data['order'] == undefined){
          Requ.showToast('暂无订单');
          this.setData({
            data: res.data,
          });
          return;
        }
        let orderList = new Array();
        res.data.order.map((e)=>{
          let data = {
            goods_name: e.goods_name,
            msg: e.msg,
            order_goodsnum: e.order_goodsnum,
            order_id: e.order_id,
            order_image: e.order_image,
            order_num: e.order_num,
            order_payment: e.order_payment,
            order_price: e.order_price,
            order_shop: e.order_shop,
            order_shopid: e.order_shopid,
            order_userid: e.order_userid,
            state:e.state,
          };
          if(e.state == 8 && current == 1){ // 待付款
            orderList.push(data);
          }else if(e.state == 9 && current == 2){
            orderList.push(data);
          }else if(e.state == 9 && current == 3){
            orderList.push(data);
          }else if(current == 0){
            orderList.push(data);
          }else if(e.state == 7 && current == 4){ // 未评价
            orderList.push(data);
          }else if(e.state == 6 && current == 4){ // 已评价
            orderList.push(data);
          }
        });
        if(orderList == '' || orderList == false){
          Requ.showToast('暂无更多订单')
          return;
        }
        console.log(orderList);
        this.OutOrderList(current, orderList);
        return;
        var orderH = order.length * 360
        that.setData({
          data: res.data,
          order: res.data.order,
          winHeight: orderH
        });
      });
      return;
    if(current == 2){
      this.setData({
        order2: orderList,
        winHeight: orderList.length * 360
      })
    }
    console.log(orderList.length);

  },
  OutOrderList: function(current, order){
    var orderH = order.length * 360;
    var that = this;
    switch (current) {
      case 0:
        that.setData({
          order: order,
          winHeight: orderH
        })
        break;
      case 1:
        that.setData({
          order1: order,
          winHeight: orderH
        })
        break;
      case 2:
        that.setData({
          order2: order,
          winHeight: orderH
        })
        break;
      case 3:
        that.setData({
          order3: order,
          winHeight: orderH
        })
        break;
      case 4:
        that.setData({
          order4: order,
          winHeight: orderH
        })
      case 5:
        that.setData({
          order5: order,
          winHeight: orderH
        })
        break;
      case 6:
        that.setData({
          order6: order,
          winHeight: orderH
        })
        break;
      case 7:
        that.setData({
          order7: order,
          winHeight: orderH
        })
    }
  },
  bindChange: function (e) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this
    var thisid = e.detail.current
    abc = e.detail.current
    that.setData({
      currentTab: thisid
    })
    this.GetUserOrderList(thisid);
    return
    new Request('get_user_orderlist.cls.php').request(
      () => {
        return {
          data: thisid,
          num: 0,
          openid: app.globalData.openid,
        }
      },
      (res) => {
        wx.hideLoading()
        if (res.data.show == 5) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000,
            mask: false
          })
          this.onShow()
        } else {
          var orderH = 0
          var order = res.data.order
          if (order) {
            orderH = order.length * 360
            switch (thisid) {
              case 0:
                that.setData({
                  order: order,
                  winHeight: orderH
                })
                break;
              case 1:
                that.setData({
                  order1: order,
                  winHeight: orderH
                })
                break;
              case 2:
                that.setData({
                  order2: order,
                  winHeight: orderH
                })
                break;
              case 3:
                that.setData({
                  order3: order,
                  winHeight: orderH
                })
                break;
              case 4:
                that.setData({
                  order4: order,
                  winHeight: orderH
                })
              case 5:
                that.setData({
                  order5: order,
                  winHeight: orderH
                })
                break;
              case 6:
                that.setData({
                  order6: order,
                  winHeight: orderH
                })
                break;
              case 7:
                that.setData({
                  order7: order,
                  winHeight: orderH
                })
            }
          }
          
        }
      });
  },
  // 点击tab切换 
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  bindViewTap: function (e) {
    return;
    wx.navigateTo({
      url: '../order_details/order_details?orderid=' + e.currentTarget.dataset.orderid
    })
  },
  // onPullDownRefresh: function () {
  //     wx.startPullDownRefresh()
  //     setTimeout(function () {
  //         wx.stopPullDownRefresh()
  //     }, 1500);
  // },

  // 页面触底事件
  onReachBottom: function () {
    var that = this;
    // 订单数+10
    num = num + 10;
    this.GetUserOrderList(this.data.currentTab)
    return;
    new Request('get_user_orderlist.cls.php').request(
      () => {
        return {
          data: that.data.currentTab,
          num: num,
          openid: app.globalData.openid,
        }
      },
    (res) => {
      if (res.data.msg == "已经到底了") {
        wx.showToast({
          title: '已经到底了',
          icon: 'loading',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '加载中',
          icon: 'loading',
          duration: 2000
        })
      }
      var order = res.data.order
      var orderH = order.length * 360
      that.setData({
        order: res.data.order,
        winHeight: orderH
      });
    });
  },
  // 取消订单
  oncancel: function (e) {
    var that = this
    new Request('updata_order_delete.cls.php').request(
      () => {
        return {
          order_id: e.currentTarget.dataset.orderid,
          openid: app.globalData.openid,
        }
      },
    (res) => {
      wx.showToast({
        title: '订单已取消',
        icon: 'none',
        duration: 1500,
        mask: true
      })
      setTimeout(function () {
        that.onShow()
      }, 1500)
    });
  },
  // 跳转店铺
  gotoshop: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../new_shop/new_shop?MS_id=' + e.currentTarget.dataset.shopid
    })
  },
  // 跳转支付页面
  onpay: function (e) {
    getApp().globalData.orderid = e.currentTarget.dataset.orderid;
     wx.switchTab({
      url: '/pages/self_take/self_take'
      /* url: '/pages/self_take/self_take?orderid=' + res.data.orderid */
    });
    return;
    wx.navigateTo({
      url: '../self_take/self_take?orderid=' + e.currentTarget.dataset.orderid
    })
  },
  // 催单
  onreminder: function (e) {
    // wx.showLoading({
    //     title: '催单已成功',
    //     mask: 'success'
    // })
    wx.showToast({
      title: '催单已成功',
      icon: 'none',
      duration: 1500,
      mask: true
    })
  },
  onfinish: function (e) {
    wx.navigateTo({
      url: '/pages/score/score?orderid=' + e.currentTarget.dataset.orderid
    })
  },
  onevaluate: function (e) {
    wx.navigateTo({
      url: '/pages/score/score?orderid=' + e.currentTarget.dataset.orderid
    })
  },
  // 再来一单
  onorder: function (e) {
    new Request('create_order_again.cls.php').request(
      () => {
        return {
          order_id: e.currentTarget.dataset.orderid,
          openid: app.globalData.openid,
        }
      },
    (res) => {
      wx.navigateTo({
        url: '/pages/pay/pay?orderid=' + res.data.orderid
      })
    });
  },
})